package com.fingard.dsp.bank.directbank.zyb01;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextWriter;


public class ZYB01Base extends DirectBase {

    protected void writeCommonHeader(XmlTextWriter xmlWriter, ZYB01Header p_bankHeader, String erpJnlNo) throws Exception {
        /*
<?xml version="1.0" encoding="UTF-8"?>
<Message>
 <Sys_Head>
    <TrsCode>05B2EActBalQry</TrsCode>
    <ERPTrsTimestamp>20170217182205916</ERPTrsTimestamp>
    <ERPJnlNo>1487302874867</ERPJnlNo>
    <WsId>10.64.16.85</WsId>
 </Sys_Head>
		 * */
        if (StringHelper.isNullOrEmpty(p_bankHeader.trsCode)) {
            throw new Exception("δ��ֵtrsCode");
        }
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String ERPTrsTimestamp = sdf.format(date);
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        xmlWriter.writeStartElement("Message");
        xmlWriter.writeStartElement("Sys_Head");
        xmlWriter.writeElementString("TrsCode", p_bankHeader.trsCode);
        xmlWriter.writeElementString("ERPTrsTimestamp", ERPTrsTimestamp);
        xmlWriter.writeElementString("ERPJnlNo", erpJnlNo);
        xmlWriter.writeElementString("WsId", bankFront.serverIP);
        xmlWriter.writeEndElement();
    }

    protected String[] sendToBank(String p_reqStr, ZYB01Header bankHeader, String sigData, String charsetName) throws Exception {

        String[] retStr = new String[]{"", ""};
        String trsCode = bankHeader.trsCode;
        String httpUrl = "";
        httpUrl = bankFront.serverURL + bankHeader.trsCode + ".do?userPassword=" + bankFront.loginPasswd + "&SIGDATA=" + sigData;
        WriteBankLogLn("2.�ύ���еĵ�ַ��" + httpUrl);
        WriteBankLogStep2(p_reqStr);

        //��������ǰ��
        WebRequest tmpRequest = new WebRequest(httpUrl);
        String charName = "GBK";
        byte[] tmpWriteBytes = p_reqStr.getBytes(charsetName);
        retStr = tmpRequest.upload(tmpWriteBytes, charName);
        WriteBankLogStep3(retStr[1]);

        return retStr;
    }

    protected static boolean isActiveRetCode(String retCode) {
        if (retCode != null) {
            if (retCode.equals("POK") || retCode.equals("000000") || retCode.equals("OK") || retCode.equals("BH")) {
                return true;
            }
        }
        return false;
    }
}
